//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "CPIBond.h"
using namespace Cephei::QL::Instruments::Bonds;
#include <gen/QL/Times/Period.h>
#include <gen/QL/Indexes/ZeroInflationIndex.h>
#include <gen/QL/Times/Schedule.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/CashFlow.h>
#include <gen/QL/PricingEngine.h>
#include <gen/QL/Instruments/Bond.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Indexes;
using namespace Cephei::QL;
using namespace Cephei::QL::Instruments;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Instruments::Bonds::CCPIBond::CCPIBond (UInt32 settlementDays, Double faceAmount, Boolean growthOnly, Double baseCPI, Cephei::QL::Times::IPeriod^ observationLag, Cephei::QL::Indexes::IZeroInflationIndex^ cpiIndex, QL::Cashflows::CPI::InterpolationTypeEnum observationInterpolation, Cephei::QL::Times::ISchedule^ schedule, Cephei::Core::IVector<Double>^ coupons, Cephei::QL::Times::IDayCounter^ accrualDayCounter, Microsoft::FSharp::Core::FSharpOption<QL::Times::BusinessDayConventionEnum>^ paymentConvention, Microsoft::FSharp::Core::FSharpOption<DateTime>^ issueDate, Cephei::QL::IPricingEngine^ QL_Pricer) : CBond(CCPIBond::typeid)
{
    CPeriod^ _CobservationLag;
    CZeroInflationIndex^ _CcpiIndex;
    CSchedule^ _Cschedule;
    CDayCounter^ _CaccrualDayCounter;
    try
    {
#ifdef HANDLE
        _phCPIBond = NULL;
#endif
        QuantLib::Natural _settlementDays = (QuantLib::Natural)ValueHelper::Convert (settlementDays); //d
        QuantLib::Real _faceAmount = (QuantLib::Real)ValueHelper::Convert (faceAmount); //d
        bool _growthOnly = (bool)ValueHelper::Convert (growthOnly); //d
        QuantLib::Real _baseCPI = (QuantLib::Real)ValueHelper::Convert (baseCPI); //d
        _CobservationLag = safe_cast<CPeriod^> (observationLag);
        _CobservationLag->Lock();
        QuantLib::Period& _observationLag = static_cast<QuantLib::Period&> (_CobservationLag->GetReference ()); 
        _CcpiIndex = safe_cast<CZeroInflationIndex^> (cpiIndex);
        _CcpiIndex->Lock();
        boost::shared_ptr<QuantLib::ZeroInflationIndex>& _cpiIndex = static_cast<boost::shared_ptr<QuantLib::ZeroInflationIndex>&> (_CcpiIndex->GetShared ()); 
        QuantLib::CPI::InterpolationType _observationInterpolation = (QuantLib::CPI::InterpolationType)observationInterpolation ;
        _Cschedule = safe_cast<CSchedule^> (schedule);
        _Cschedule->Lock();
        QuantLib::Schedule& _schedule = static_cast<QuantLib::Schedule&> (_Cschedule->GetReference ()); 
        coupons->Lock();
        INativeVector<Double>^ _NCIcoupons = coupons->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCcoupons = safe_cast<CDoubleVector^>(_NCIcoupons);
        std::vector<QuantLib::Rate>& _coupons = static_cast<std::vector<QuantLib::Rate>&> (_NCcoupons->GetReference ());
        _CaccrualDayCounter = safe_cast<CDayCounter^> (accrualDayCounter);
        _CaccrualDayCounter->Lock();
        QuantLib::DayCounter& _accrualDayCounter = static_cast<QuantLib::DayCounter&> (_CaccrualDayCounter->GetReference ()); 
        QuantLib::BusinessDayConvention _paymentConvention = 
            (Microsoft::FSharp::Core::FSharpOption<QL::Times::BusinessDayConventionEnum>::IsSome::get (paymentConvention) ? (QuantLib::BusinessDayConvention)paymentConvention->Value : QuantLib::BusinessDayConvention::ModifiedFollowing); //10
        QuantLib::Date _issueDate = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (issueDate) ? (QuantLib::Date)ValueHelper::Convert (issueDate->Value) : QuantLib::Date()); //4
        _ppCPIBond = new boost::shared_ptr<QuantLib::CPIBond> (new QuantLib::CPIBond ( _settlementDays,  _faceAmount,  _growthOnly,  _baseCPI,  _observationLag,  _cpiIndex,  _observationInterpolation,  _schedule,  _coupons,  _accrualDayCounter,  _paymentConvention,  _issueDate ));
        CPricingEngine^ _CQL_Pricer = safe_cast<CPricingEngine^> (QL_Pricer);
        boost::shared_ptr<QuantLib::PricingEngine>& _QL_Pricer = static_cast<boost::shared_ptr<QuantLib::PricingEngine>&> (_CQL_Pricer->GetShared ());
        (*_ppCPIBond)->setPricingEngine (_QL_Pricer);
        SetBond (boost::dynamic_pointer_cast<QuantLib::Bond> (*_ppCPIBond));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CobservationLag != nullptr) _CobservationLag->Unlock();
        if (_CcpiIndex != nullptr) _CcpiIndex->Unlock();
        if (_Cschedule != nullptr) _Cschedule->Unlock();
        if (coupons != nullptr) coupons->Unlock();    //not optional
        if (_CaccrualDayCounter != nullptr) _CaccrualDayCounter->Unlock();
    }
}
Cephei::QL::Instruments::Bonds::CCPIBond::CCPIBond (boost::shared_ptr<QuantLib::CPIBond>& childNative, Object^ owner) : CBond(CCPIBond::typeid)
{
#ifdef HANDLE
	_phCPIBond = NULL;
#endif
	_ppCPIBond = &childNative;
    _ppBond = new boost::shared_ptr<QuantLib::Bond> (boost::dynamic_pointer_cast<QuantLib::Bond> (*_ppCPIBond));
}
Cephei::QL::Instruments::Bonds::CCPIBond::CCPIBond (QuantLib::CPIBond& childNative, Object^ owner) : CBond(CCPIBond::typeid)
{
#ifdef HANDLE
	_phCPIBond = NULL;
#endif
	_ppCPIBond = new boost::shared_ptr<QuantLib::CPIBond> (&childNative);
    _ppBond = new boost::shared_ptr<QuantLib::Bond> (boost::dynamic_pointer_cast<QuantLib::Bond> (*_ppCPIBond));
    _CPIBondOwner = owner;
    _BondOwner = owner;
}

Cephei::QL::Instruments::Bonds::CCPIBond::CCPIBond (CCPIBond^ copy) : CBond(CCPIBond::typeid)
{
#ifdef HANDLE
	_phCPIBond = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppCPIBond = new boost::shared_ptr<QuantLib::CPIBond> (copy->GetShared());
        _ppBond = new boost::shared_ptr<QuantLib::Bond> (boost::dynamic_pointer_cast<QuantLib::Bond> (*_ppCPIBond));
    }
}
Cephei::QL::Instruments::Bonds::CCPIBond::CCPIBond (PLATFORM::Type^ t) : CBond(CCPIBond::typeid)
{
#ifdef HANDLE
	_phCPIBond = NULL;
#endif
	if (!t->IsSubclassOf(CCPIBond::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Instruments::Bonds::CCPIBond::CCPIBond (QuantLib::Handle<QuantLib::CPIBond>& childNative, Object^ owner)  : CBond(CCPIBond::typeid)
{
	_phCPIBond = &childNative;
	_ppCPIBond = &static_cast<boost::shared_ptr<QuantLib::CPIBond>>(childNative.currentLink());
    _ppBond = new boost::shared_ptr<QuantLib::Bond> (boost::dynamic_pointer_cast<QuantLib::Bond> (*_ppCPIBond));
    _CPIBondOwner = owner;
}
Cephei::QL::Instruments::Bonds::CCPIBond::CCPIBond (QuantLib::Handle<QuantLib::CPIBond> childNative)  : CBond(CCPIBond::typeid)
{
	_phCPIBond = &childNative;
	_ppCPIBond = &static_cast<boost::shared_ptr<QuantLib::CPIBond>>(childNative.currentLink());
    _ppBond = new boost::shared_ptr<QuantLib::Bond> (boost::dynamic_pointer_cast<QuantLib::Bond> (*_ppCPIBond));
}
#endif
#ifdef STRUCT
Cephei::QL::Instruments::Bonds::CCPIBond::CCPIBond (QuantLib::CPIBond childNative)  : CBond(CCPIBond::typeid)
{
#ifdef HANDLE
	_phCPIBond = NULL;
#endif
	_ppCPIBond = new boost::shared_ptr<QuantLib::CPIBond> (new QuantLib::CPIBond (childNative));
    _ppBond = new boost::shared_ptr<QuantLib::Bond> (boost::dynamic_pointer_cast<QuantLib::Bond> (*_ppCPIBond));
}
#endif

Cephei::QL::Instruments::Bonds::CCPIBond::~CCPIBond ()
{
    if (_ppCPIBond != NULL)
    {
	    delete _ppCPIBond;
        _ppCPIBond = NULL;
    }
}
Cephei::QL::Instruments::Bonds::CCPIBond::!CCPIBond ()
{
    if (_ppCPIBond != NULL)
    {
	    delete _ppCPIBond;
    }
}
QuantLib::CPIBond& Cephei::QL::Instruments::Bonds::CCPIBond::GetReference ()
{
    if (_ppCPIBond == NULL) throw REFNEW NativeNullException ();
	return **_ppCPIBond;
}
boost::shared_ptr<QuantLib::CPIBond>& Cephei::QL::Instruments::Bonds::CCPIBond::GetShared ()
{
    if (_ppCPIBond == NULL) throw REFNEW NativeNullException ();
	return *_ppCPIBond;
}
QuantLib::CPIBond* Cephei::QL::Instruments::Bonds::CCPIBond::GetPointer ()
{
    if (_ppCPIBond == NULL) throw REFNEW NativeNullException ();
	return &**_ppCPIBond;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::CPIBond>& Cephei::QL::Instruments::Bonds::CCPIBond::GetHandle ()
{
	if (_phCPIBond == NULL)
	{
		_phCPIBond = new Handle<QuantLib::CPIBond> (*_ppCPIBond);
	}
	return *_phCPIBond;
}
#endif
bool Cephei::QL::Instruments::Bonds::CCPIBond::HasNative () 
{
	return (_ppCPIBond != NULL);
}

Double Cephei::QL::Instruments::Bonds::CCPIBond::BaseCPI::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppCPIBond)->baseCPI ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Indexes::IZeroInflationIndex^ Cephei::QL::Instruments::Bonds::CCPIBond::CpiIndex::get ()
{
    try
    {
    	boost::shared_ptr<QuantLib::ZeroInflationIndex>& _rv = (boost::shared_ptr<QuantLib::ZeroInflationIndex>&)(*_ppCPIBond)->cpiIndex ( );   
        Cephei::QL::Indexes::CZeroInflationIndex^ _nrv = REFNEW Cephei::QL::Indexes::CZeroInflationIndex (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::IDayCounter^ Cephei::QL::Instruments::Bonds::CCPIBond::DayCounter::get ()
{
    try
    {
    	QuantLib::DayCounter& _rv = (QuantLib::DayCounter&)(*_ppCPIBond)->dayCounter ( );   
        Cephei::QL::Times::CDayCounter^ _nrv = REFNEW Cephei::QL::Times::CDayCounter (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
QL::Times::FrequencyEnum Cephei::QL::Instruments::Bonds::CCPIBond::Frequency::get ()
{
    try
    {
    	QuantLib::Frequency _rv = (QuantLib::Frequency)(*_ppCPIBond)->frequency ( );   
        QL::Times::FrequencyEnum _nrv = (QL::Times::FrequencyEnum)_rv;
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Instruments::Bonds::CCPIBond::GrowthOnly::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppCPIBond)->growthOnly ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
QL::Cashflows::CPI::InterpolationTypeEnum Cephei::QL::Instruments::Bonds::CCPIBond::ObservationInterpolation::get ()
{
    try
    {
    	QuantLib::CPI::InterpolationType _rv = (QuantLib::CPI::InterpolationType)(*_ppCPIBond)->observationInterpolation ( );   
        QL::Cashflows::CPI::InterpolationTypeEnum _nrv = (QL::Cashflows::CPI::InterpolationTypeEnum)_rv;
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::IPeriod^ Cephei::QL::Instruments::Bonds::CCPIBond::ObservationLag::get ()
{
    try
    {
    	QuantLib::Period _rv = (QuantLib::Period)(*_ppCPIBond)->observationLag ( );   
        Cephei::QL::Times::CPeriod^ _nrv = REFNEW Cephei::QL::Times::CPeriod (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Instruments::Bonds::ICPIBond^ Cephei::QL::Instruments::Bonds::CCPIBond_Factory::Create (UInt32 settlementDays, Double faceAmount, Boolean growthOnly, Double baseCPI, Cephei::QL::Times::IPeriod^ observationLag, Cephei::QL::Indexes::IZeroInflationIndex^ cpiIndex, QL::Cashflows::CPI::InterpolationTypeEnum observationInterpolation, Cephei::QL::Times::ISchedule^ schedule, Cephei::Core::IVector<Double>^ coupons, Cephei::QL::Times::IDayCounter^ accrualDayCounter, Microsoft::FSharp::Core::FSharpOption<QL::Times::BusinessDayConventionEnum>^ paymentConvention, Microsoft::FSharp::Core::FSharpOption<DateTime>^ issueDate, Cephei::QL::IPricingEngine^ QL_Pricer)
{
    return REFNEW CCPIBond ( settlementDays,  faceAmount,  growthOnly,  baseCPI,  observationLag,  cpiIndex,  observationInterpolation,  schedule,  coupons,  accrualDayCounter,  paymentConvention,  issueDate,  QL_Pricer);
}
